package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.quality.entity.ProcessMaterialScrap;
import com.mxpio.erp.common.quality.service.ProcessMaterialScrapService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProcessMaterialScrapController", description = "工序材料报废接口")
@RestController
@RequestMapping("/erp/quality/process_scrap/")
public class ProcessMaterialScrapController {

    @Autowired
    private ProcessMaterialScrapService service;

    @GetMapping("list")
    @Operation(summary = "工序材料报废列表", description = "获取工序材料报废列表（无分页）", method = "GET")
    public Result<List<ProcessMaterialScrap>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProcessMaterialScrap> processMaterialScrap = service.list(ProcessMaterialScrap.class, criteria);
        return Result.OK(processMaterialScrap);
    }

    @GetMapping("page")
    @Operation(summary = "工序材料报废列表", description = "获取工序材料报废列表（分页）", method = "GET")
    public Result<Page<ProcessMaterialScrap>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProcessMaterialScrap> processMaterialScraps = service.listPage(ProcessMaterialScrap.class, page, criteria);
        return Result.OK(processMaterialScraps);
    }
    

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取工序材料报废", description = "根据ID获取工序材料报废", method = "GET")
    public Result<ProcessMaterialScrap> getById(@Parameter(description="id") @PathVariable(name = "id", required = true)String id) {
        ProcessMaterialScrap processMaterialScrap = service.getById(ProcessMaterialScrap.class, id);
        return Result.OK(processMaterialScrap);
    }

    @PostMapping("add")
    @Operation(summary = "新增工序材料报废", description = "新增工序材料报废", method = "POST")
    public Result<ProcessMaterialScrap> add(@RequestBody ProcessMaterialScrap processMaterialScrap) {
        service.save(processMaterialScrap);
        return Result.OK(processMaterialScrap);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑工序材料报废", description = "编辑工序材料报废", method = "PUT")
    public Result<ProcessMaterialScrap> edit(@RequestBody ProcessMaterialScrap processMaterialScrap) {
        ProcessMaterialScrap scrap = service.getById(ProcessMaterialScrap.class, processMaterialScrap.getId());
        if (StringUtils.equals(CommonEnums.YesNo.YES.getCode(),scrap.getDataStatus())){
            return Result.error("数据已确认提交，不能修改");
        }
        service.update(processMaterialScrap);
        return Result.OK(processMaterialScrap);
    }

    @DeleteMapping("remove/{ids}")	
    @Operation(summary = "根据id删除工序材料报废", description = "删除工序材料报废,多个id用英文逗号分隔", method = "DELETE")
    public Result<ProcessMaterialScrap> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            service.remove(ProcessMaterialScrap.class, key);
        }
        return Result.OK();
    }

    @GetMapping("confirm/{id}")
    @Operation(summary = "根据ID确定数据", description = "根据ID确定数据", method = "GET")
    public Result<ProcessMaterialScrap> confirm(@Parameter(description="id") @PathVariable(name = "id", required = true)String id) {
        ProcessMaterialScrap scrap = service.getById(ProcessMaterialScrap.class, id);
        if (StringUtils.equals(CommonEnums.YesNo.YES.getCode(),scrap.getDataStatus())){
            return Result.error("数据已提交确认，不能重复操作");
        }
        scrap.setDataStatus(CommonEnums.YesNo.YES.getCode());
        service.update(scrap);
        return Result.OK(scrap);
    }
}
